package aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Random;

@Aspect
@Component
public class PrintAspect {
    private static final Logger log = LoggerFactory.getLogger(PrintAspect.class);
    Random random = new Random();
    @Pointcut("execution(* service..*.*(..))")
    public void pointCut() {}
    @Before(value = "pointCut()")
    public void before1(JoinPoint jp) {
        int num = random.nextInt(10);
        if(num>=5&&num<=7){
            throw new RuntimeException("卡片异常");
        }else if(num>7&&num<=9){
            throw new RuntimeException("墨盒无墨");
        }
        log.info(jp.getSignature().getName() + "检查正常");
    }
    @AfterReturning(value = "pointCut()",returning = "result")
    public void afterReturning1(JoinPoint jp, Object result) {
        log.info(jp.getSignature().getName() + "打印成功" + result);
    }
    @AfterThrowing(value ="pointCut()",throwing = "e")
    public void afterThrowing1(JoinPoint jp, Throwable e) {
        log.info("异常原因: " + e.getMessage());
    }
}
